package virtualstock.player;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import virtualstock.exception.GameNotFoundException;
import virtualstock.exception.InvalidParameterException;
import virtualstock.exception.PlayerNotFoundException;
import virtualstock.exception.PortfolioNotFoundException;
import virtualstock.request.GameController;
import virtualstock.util.GameDetails;

/**
 * Servlet implementation class CreateNewGameServlet
 */
public class CreateNewGameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	private GameController gameController;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateNewGameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private void createNewGame(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	
    	String gameName = (String) request.getParameter("name");
		Long creator = new Long((String) request.getParameter("creator"));
//		int maxPlayer = Integer.parseInt((String) request.getParameter("maxPlayer"));
		int maxPlayer = 1;
		BigDecimal initialBalance = new BigDecimal((String) request.getParameter("initialBalance"));
		Date startDate = new Date();
    	
    	GameDetails details = new GameDetails(gameName, creator, maxPlayer, initialBalance, startDate);
    	
    	try {
			Long gameID = gameController.createNewGame(details);
			gameController.addPlayerToGame(creator, gameID);
			out.println("Game created succefully!");
		} catch (PlayerNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortfolioNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getSession().setAttribute("playerDetails", request.getSession().getAttribute("playerDetails"));
		response.sendRedirect("playerHomePage.jsp");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		createNewGame(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		createNewGame(request,response);
	}

}
